package com.ruoyi.manage.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.manage.domain.EventSurgeryDetail;
import com.ruoyi.manage.mapper.EventSurgeryMapper;
import com.ruoyi.manage.domain.EventSurgery;
import com.ruoyi.manage.service.IEventSurgeryService;

/**
 * 手术事件Service业务层处理
 *
 * @author zww
 * @date 2025-07-01
 */
@Service
public class EventSurgeryServiceImpl implements IEventSurgeryService {
    @Autowired
    private EventSurgeryMapper eventSurgeryMapper;

    /**
     * 查询手术事件
     *
     * @param eventSurgeryId 手术事件主键
     * @return 手术事件
     */
    @Override
    public EventSurgery selectEventSurgeryByEventSurgeryId(Long eventSurgeryId) {
        if (eventSurgeryId == null) {
            return null;
        }
        EventSurgery eventSurgery = eventSurgeryMapper.selectEventSurgeryByEventSurgeryId(eventSurgeryId);
        eventSurgery.setEventSurgeryDetailList(eventSurgeryMapper.selectEventSurgeryDetailList(eventSurgeryId));
        return eventSurgery;
    }

    /**
     * 查询手术事件列表
     *
     * @param eventSurgery 手术事件
     * @return 手术事件
     */
    @Override
    public List<EventSurgery> selectEventSurgeryList(EventSurgery eventSurgery) {
        return eventSurgeryMapper.selectEventSurgeryList(eventSurgery);
    }

    /**
     * 新增手术事件
     *
     * @param
     * @return 结果
     */
    @Transactional
    @Override
    public void insertEventSurgery(List<EventSurgery> eventSurgerys) {

        eventSurgerys.forEach(eventSurgery -> {

            if (eventSurgery.getEventSurgeryId() != null) {
                updateEventSurgery(eventSurgery);
            } else {
                eventSurgeryMapper.insertEventSurgery(eventSurgery);
                insertEventSurgeryDetail(eventSurgery);
            }

        });

    }

    @Transactional
    public Long insertEventSurgery(EventSurgery eventSurgery) {
        if (eventSurgery.getEventSurgeryId() != null) {
             updateEventSurgery(eventSurgery);
        } else {
              eventSurgeryMapper.insertEventSurgery(eventSurgery);
            insertEventSurgeryDetail(eventSurgery);
        }
        return eventSurgery.getEventSurgeryId();
    }

    /**
     * 修改手术事件
     *
     * @param eventSurgery 手术事件
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEventSurgery(EventSurgery eventSurgery) {
        eventSurgeryMapper.deleteEventSurgeryDetailByEventSurgeryId(eventSurgery.getEventSurgeryId());
        insertEventSurgeryDetail(eventSurgery);
        return eventSurgeryMapper.updateEventSurgery(eventSurgery);
    }

    /**
     * 批量删除手术事件
     *
     * @param eventSurgeryIds 需要删除的手术事件主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEventSurgeryByEventSurgeryIds(Long[] eventSurgeryIds) {
        eventSurgeryMapper.deleteEventSurgeryDetailByEventSurgeryIds(eventSurgeryIds);
        return eventSurgeryMapper.deleteEventSurgeryByEventSurgeryIds(eventSurgeryIds);
    }

    /**
     * 删除手术事件信息
     *
     * @param eventSurgeryId 手术事件主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEventSurgeryByEventSurgeryId(Long eventSurgeryId) {
        eventSurgeryMapper.deleteEventSurgeryDetailByEventSurgeryId(eventSurgeryId);
        return eventSurgeryMapper.deleteEventSurgeryByEventSurgeryId(eventSurgeryId);
    }

    /**
     * 新增手术事件详情信息
     *
     * @param eventSurgery 手术事件对象
     */
    public void insertEventSurgeryDetail(EventSurgery eventSurgery) {
        List<EventSurgeryDetail> eventSurgeryDetailList = eventSurgery.getEventSurgeryDetailList();
        Long eventSurgeryId = eventSurgery.getEventSurgeryId();
        if (StringUtils.isNotNull(eventSurgeryDetailList)) {
            List<EventSurgeryDetail> list = new ArrayList<EventSurgeryDetail>();
            for (EventSurgeryDetail eventSurgeryDetail : eventSurgeryDetailList) {
                eventSurgeryDetail.setEventSurgeryId(eventSurgeryId);
                list.add(eventSurgeryDetail);
            }
            if (list.size() > 0) {
                eventSurgeryMapper.batchEventSurgeryDetail(list);
            }
        }
    }
}
